package com.joinval.stamp;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;

/**
 * 该测试方法为签章平台接口调用通用方法，根据接口参数、地址相应调整即可
 */
public class StampTest {

	public static void main(String[] args) throws Exception {
		ZWWW();
	}

	/**
	 * 政务外网
	 * 
	 * @throws Exception
	 */
	public static void ZWWW() throws Exception {
		String accessToken = "MZiKtq9dxe";
		String accessSecret = "T7eTnDFCCh8DMq3VQ3huymALFI3H1w";
		String url = "https://open-out.qiyuesuo.cn/sync/create/company/autosign";
		HttpClient httpClient = new HttpClient(Optional.empty());

		String x_qys_timestamp = String.valueOf(System.currentTimeMillis());
		// 签名，对参数进行md5运算，转成16进制字符串：
		String x_qys_signature = MD5.toMD5(accessToken + accessSecret + x_qys_timestamp);

		// 拼接参数
		HttpParamers paramers = new HttpParamers(HttpMethod.POST);
		File file = new File("/Users/chenkaihang/IdeaProjects/joinval-volunteer/src/main/resources/pdf/111.pdf");
		FileInputStream inputStream = new FileInputStream(file);
		StreamFile streamFile = new StreamFile(inputStream);
		paramers.addFile("file", streamFile);
//	        paramers.addParam("id", "310109199307143011");
		paramers.addParam("tenantName", "上海市数字证书认证中心有限公司");
		paramers.addParam("personName", "上海市数字证书认证中心有限公司");
		paramers.addParam("contact", "16621097886");
		paramers.addParam("needVerify", "false");
//	        paramers.addParam("cardno", "310109199307143011");
		//paramers.addParam("sealData", sealData);
		paramers.addParam("sealIds","2697387109723365478");
		paramers.addParam("stamperStr",
				"[{\"offsetY\":\"-0.1\",\"offsetX\":\"-0.1\",\"keyword\":\"测试\",\"sealId\":\"2697387109723365478\"}]");

		// 指定关键字👇
//		paramers.addParam("stamperStr",
//				"[{\"keyword\":\"上海市松江区水务局\",\"sealCenter\":true,\"sealId\":2690766800312786952}]");
		//paramers.addParam("callback","http://wonders.vaiwan.com/onlineservice/nj/manager/signCallBack");
		paramers.addParam("callback","http://192.168.1.60:5050/sys/sysVolBase/testCallBack");
		// 政务外网印章id 2690766800312786952
		// 测试环境印章id:2697387109723365478
		// 添加请求头
		HttpHeader httpHeader = new HttpHeader();
		httpHeader.addProperty("x-qys-timestamp", x_qys_timestamp);
		httpHeader.addProperty("x-qys-signature", x_qys_signature);
		httpHeader.addProperty("x-qys-accesstoken", accessToken);
		System.out.println(x_qys_timestamp);
		System.out.println(x_qys_signature);
		long startTime = System.currentTimeMillis(); // 获取开始时间
		String response = httpClient.doPost(url, paramers, httpHeader);
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("程序运行时间：" + (endTime - startTime) + "ms"); // 输出程序运行时间
		System.out.println(response);

	}


	public static String readValue(String fileName, String key) {
		Properties props = new Properties();
		String value = null;
		try {
			// 配置文件位于当前目录中的config目录下
			InputStream in = new BufferedInputStream(new FileInputStream("config/" + fileName));
			props.load(in);
			value = props.getProperty(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}
}
